package com.example.spring_project.controller;
import com.example.spring_project.entity.LoginBody;
import com.example.spring_project.service.CaptchaService;
import com.example.spring_project.service.UserService;
import com.example.spring_project.utils.ImageUtil;
import com.example.spring_project.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;


@RestController
@Api(value = "登录",tags = {"登录访问接口"})
public class LoginController {
    @Autowired
    private  UserService userService;
    @Autowired
    private  CaptchaService captchaService;


    /**
     * 根据用户名和密码进行登陆验证，并返回登录后的User对象。如果验证成功，则返回200 OK和User对象；否则返回401 Unauthorized和错误信息。
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录接口", notes = "根据用户名和密码进行登录验证，并返回登录后的User对象。")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "登录成功，返回User对象。", response = Result.class),
            @ApiResponse(code = 401, message = "未经授权或登录验证失败。", response = Result.class)
    })
    public Result<Object> login(@RequestBody LoginBody loginBody) {
        //SSO应该返回一个token
        return userService.login(loginBody); // 调用 UserService 的 login() 方法进行登陆验证;
    }

    @RequestMapping(value = "/generateCaptcha",method = RequestMethod.GET)//验证码
    @ApiOperation(value = "生成验证码接口", notes = "生成验证码图片并返回。")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "验证码生成成功，返回验证码图片。", response = Result.class),
            @ApiResponse(code = 500, message = "验证码生成失败。", response = Result.class)
    })
    public Result<Object> generateImage(
                              HttpServletResponse response) throws Exception {
        return captchaService.generateCaptcha();
    }

}

